package com.iplant.wms.serviceImpl.dao.wpo;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.po.excel.ExcelData;
import com.iplant.base.po.excel.ExcelLineData;
import com.iplant.base.po.excel.ExcelSheetData;
import com.iplant.base.utils.annotation.dao.DBBaseDAO;
import com.iplant.base.utils.cache.CacheUtils;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.mss.MSSMaterial;
import com.iplant.basic.service.po.mss.MSSUnit;
import com.iplant.wms.service.po.wpo.WMSPurchaseOrderItem;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
@DBBaseDAO(WMSPurchaseOrderItem.class)
public class WMSPurchaseOrderItemDAO extends BaseDAO {


    private static WMSPurchaseOrderItemDAO Instance = null;

    private WMSPurchaseOrderItemDAO() {
        super("");
        this.Init();
    }

    public static WMSPurchaseOrderItemDAO getInstance() {
        if (Instance == null) {
            Instance = new WMSPurchaseOrderItemDAO();
            Instance.Init();
        }
        return Instance;
    }


    /**
     * 删除集合
     *
     * @param wOrderID
     */
    public void WMS_DeletePurchaseOrderItemList(BMSEmployee wLoginUser, int wOrderID,
                                                OutResult<Integer> wErrorCode) {
        try {
            if (wOrderID <= 0) {
                wErrorCode.set(ExceptionTypes.Parameter.getValue());
                return;
            }
            Map<String, Object> wDeleteParams = new HashMap<>();
            wDeleteParams.put("PurchaseOrderID", wOrderID);
            this.Delete(wDeleteParams, " and ID>0 ");

        } catch (Exception ex) {
            wErrorCode.set(ExceptionTypes.DBSQL.getValue());
            log.error("WMS_DeletePurchaseOrderItemList", ex);
        }

    }


    public List<WMSPurchaseOrderItem> WMS_QueryPurchaseOrderItemListByPurchaseOrder(
            BMSEmployee wLoginUser, int wPurchaseOrderID, String wPurchaseOrderCode, Pagination wPagination, OutResult<Integer> wErrorCode) {

        List<WMSPurchaseOrderItem> wResultList = new ArrayList<>();


        if (wPurchaseOrderID <= 0 || StringUtils.isEmpty(wPurchaseOrderCode)) {

            return wResultList;
        }
        Map<String, Object> wParams = new HashMap<>();

        wParams.put("PurchaseOrderID", wPurchaseOrderID);

        wParams.put("PurchaseOrderCodeEQ", wPurchaseOrderCode);


        return SelectPOList(wLoginUser, wParams, wPagination, wErrorCode);

    }



    /**
     * Excel数据解析-采购订单明细
     */
    public List<WMSPurchaseOrderItem> GetWMSPurchaseOrderItemList(BMSEmployee wLoginUser, ExcelData wExcelData) {
        List<WMSPurchaseOrderItem> wResult = new ArrayList<WMSPurchaseOrderItem>();
        try {
            if (wExcelData.sheetData == null || wExcelData.sheetData.size() <= 1) {
                return wResult;
            }

            ExcelSheetData wSheetData = wExcelData.sheetData.get(1);
            if (wSheetData.lineData == null || wSheetData.lineData.size() <= 0) {
                return wResult;
            }

            int wRow = 0;
            MSSUnit wMSSUnit;
            MSSMaterial wMSSMaterial;
            for (ExcelLineData wExcelLineData : wSheetData.lineData) {
                if (wRow == 0) {
                    wRow++;
                    continue;
                }

                String wPONo = wExcelLineData.colData.get(0);
                String wRowNo = wExcelLineData.colData.get(1);
                String wMaterialNo = wExcelLineData.colData.get(2);
                String wMaterialName = wExcelLineData.colData.get(3);
                String wNumber = wExcelLineData.colData.get(4);
                String wUnit = wExcelLineData.colData.get(5);
                String wPrice = wExcelLineData.colData.get(6);
                String wLott = wExcelLineData.colData.get(7);
                String wNotes = wExcelLineData.colData.get(8);

                WMSPurchaseOrderItem wWMSPurchaseOrderItem = new WMSPurchaseOrderItem();
                wWMSPurchaseOrderItem.ID = 0;
                wWMSPurchaseOrderItem.PurchaseOrderCode = wPONo;
                wWMSPurchaseOrderItem.Code = wRowNo;
                wWMSPurchaseOrderItem.MaterialCode = wMaterialNo;
                wWMSPurchaseOrderItem.MaterialName = wMaterialName;
                wWMSPurchaseOrderItem.PackageRuleCode = wNumber;
                wWMSPurchaseOrderItem.ReceiveUnitText = wUnit;
                wMSSMaterial = CacheUtils.GetCacheInfo(MSSMaterial.class, -1, wMaterialNo);
                wWMSPurchaseOrderItem.MaterialID = wMSSMaterial.ID;
                wMSSUnit = CacheUtils.GetCacheInfo(MSSUnit.class, -1, wUnit);
                wWMSPurchaseOrderItem.ReceiveUnit = wMSSUnit.ID;

                wWMSPurchaseOrderItem.TotalPrice = StringUtils.parseDouble(wPrice);
                wWMSPurchaseOrderItem.Description = wNotes;

                wResult.add(wWMSPurchaseOrderItem);
            }
        } catch (Exception e) {
            log.error(e.toString());
        }
        return wResult;
    }


    public void WMS_UpdatePurchaseOrderFQTY(BMSEmployee wLoginUser, List<Integer> wIDList, OutResult<Integer> wErrorCode){
        wErrorCode.set(0);
        try {
            String wSQLText= StringUtils.Format("update {0} t set t.ReceivedMaterialNumber= " +
                            "( select ifnull( sum(t2.StockFQTY),0) from {1} t2 WHERE t.ID=t2.PurchaseOrderItemID) where  t.ID IN (2) ",
                    this.getTableName(),this.getTableName("wms_receivebillitem"),StringUtils.Join(",",wIDList) );

            this.Update(wSQLText,new HashMap<>());
        }catch (Exception e){
            wErrorCode.Result=ExceptionTypes.DBSQL.getValue();
            log.error("WMS_UpdateSalesDeliveryItemFQTY",e);
        }
    }
}
